package ApplicationStart.FastJson;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import java.util.ArrayList;
import java.util.List;

@Configuration
public class FastJsonHttpMessageConverterEx extends FastJsonHttpMessageConverter {

    private FastJsonConfig fastJsonConfig = new FastJsonConfig();
    private List<MediaType> fastMediaTypes = new ArrayList<>();

    public FastJsonHttpMessageConverterEx(){
        //这里配置 FastJson 特性
        fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");    // 自定义时间格式
        fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat); // 正常转换 null 值
        fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8); //处理中文乱码
        this.setFastJsonConfig(fastJsonConfig);
        this.setSupportedMediaTypes(fastMediaTypes);
    }

    @Bean
    public HttpMessageConverter fastJsonHttpMessageConverters(){
        FastJsonHttpMessageConverter messageConverter =  new FastJsonHttpMessageConverter();
        messageConverter.setFastJsonConfig(fastJsonConfig);
        messageConverter.setSupportedMediaTypes(fastMediaTypes);
        return messageConverter;
    }
}
